package com.aa.activity;

import org.json.JSONException;
import org.json.JSONObject;

import com.aa.Common;
import com.aa.R;
import com.aa.http.HttpOperation;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class AgentEntryActivity extends Activity implements OnClickListener{

	private final String mUrl = "http://121.9.64.196:8087/AAproject/api/mobile.php";
	private String TAG = "AgentEntryActivity";
	private Button mbtnTitleBack;
	private TextView mtvTitleCenter;
	private TextView mtvShareNum;
	private TextView mtvCurrentRank;
	private TextView mtvNextRank;
	private TextView mtvRangeRank;
	private Handler mHandler;
	private ProgressDialog mProgressDialog;
	private String mRetureErrMSG;
	private String mShareNumber;
	private String mCurClass;
	private String mNxtClass;
	private String mRangeNxtClass;
	private Button mbtnClip;
	private TextView mtvShouQuanMa;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.agent_entry);
		initView();
		
		mHandler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				
				super.handleMessage(msg);
				switch (msg.what) {
				case 0:
					Toast.makeText(AgentEntryActivity.this, mRetureErrMSG, 3000).show();
					break;
				case 1:
					mtvCurrentRank.setText(mCurClass);
					mtvNextRank.setText(mNxtClass);
					mtvShareNum.setText(mShareNumber);
					mtvRangeRank.setText(getString(R.string.app_agent_entry_3, new String[]{mRangeNxtClass}));
					break;
				case 2:
					Toast.makeText(AgentEntryActivity.this, R.string.http_connect_outtime, 3000).show();
					break;
				default:
					break;
				}
			}
			
		};
		sumbitShareNum();
	}
	
	private void initView(){
		mbtnTitleBack = (Button)findViewById(R.id.btn_title_back);
		mbtnTitleBack.setOnClickListener(this);
		mtvTitleCenter = (TextView)findViewById(R.id.tv_title_center);
		mtvTitleCenter.setText(R.string.app_agent_entry);
		mtvTitleCenter.setVisibility(View.VISIBLE);
		mtvCurrentRank = (TextView)findViewById(R.id.tv_current_rank);
		mtvNextRank = (TextView)findViewById(R.id.tv_next_rank);
		mtvShareNum = (TextView)findViewById(R.id.tv_share_num);
		mtvRangeRank = (TextView)findViewById(R.id.tv_range_rank);
		mbtnClip = (Button)findViewById(R.id.btn_clip);
		mbtnClip.setOnClickListener(this);
		mtvShouQuanMa = (TextView)findViewById(R.id.tv_shouquanma);
	}
	
	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_title_back:
			finish();
			break;
		case R.id.btn_clip:
			ClipboardManager clip = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
			clip.setText(mtvShouQuanMa.getText().toString());
			
			break;

		default:
			break;
		}
		
		
	}

private void sumbitShareNum(){
		
		mProgressDialog = ProgressDialog.show(this, getString(R.string.app_progress_title), 
				getString(R.string.app_progress_tip),true,false);
		mProgressDialog.setCancelable(true);
	
	
		final JSONObject _SendJson = new JSONObject();
		JSONObject _BodyJson = new JSONObject();
		JSONObject _HeadJson = new JSONObject();
		try {
			_HeadJson.put("v", "1.0.0");
			_HeadJson.put("time", System.currentTimeMillis());
			_HeadJson.put("cmd", 15);
			_HeadJson.put("mac", Common.iMac);
			_HeadJson.put("agent", Common.iAgentId);
								
			_BodyJson.put("uid", Common.iUID);
		
	
	
		
			_SendJson.put("head", _HeadJson);
			_SendJson.put("body", _BodyJson);
			Log.i(TAG, "sendJson = " + _SendJson);
		} catch (JSONException e) {
		
			e.printStackTrace();
		}
	
		new Thread(new Runnable() {
		
		@Override
		public void run() {
			
			String _Result = HttpOperation.postRequestJSONFormat(mUrl, _SendJson.toString());
			mProgressDialog.dismiss();
			Log.i(TAG, "return _Result = " + _Result);
			if ( _Result == null || _Result.length() == 0 ) {
				
				return;
			}
			if (_Result.equals(Common.iConnectOutTime)) {
				mHandler.sendEmptyMessage(2);
				return;
			}
				
				try {
					JSONObject _JsonObject = new JSONObject(_Result);
					
					JSONObject _Body = _JsonObject.getJSONObject("body");
					
					if(_Body.getInt("rcode") == 1) {
						mShareNumber = _Body.getString("count");
						mCurClass = _Body.getString("class1");
						mNxtClass = _Body.getString("class2");
						mRangeNxtClass = _Body.getString("upclass");		
						mHandler.sendEmptyMessage(1);
						
					}else {
						mRetureErrMSG = _Body.getString("msg");
						mHandler.sendEmptyMessage(0);
					}
				} catch (JSONException e) {						
					e.printStackTrace();
				}
			}
			
		
		}).start();
	}
	
}
